<template>
    <div>
        <el-dialog :title="isAdd ? '添加轮播图信息' : '修改轮播图信息'" :visible.sync="isShow" :before-close="cancel" center>
            <el-form :model="bannerForm" :rules="rules" ref="bannerRef">
                <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
                    <el-input v-model="bannerForm.title" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图片" :label-width="formLabelWidth">
                    <el-upload action="#" list-type="picture-card" :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemove" :file-list="fileList" :auto-upload="false" :limit="1"
                        :on-change="changePhoto">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="add">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';

import { bannerAdd, bannerEdit, bannerInfo } from '../../request/api';
export default {
    props: ['isShow', 'isAdd'],
    data() {
        return {
            dialogImageUrl: '',
            dialogVisible: false,
            fileList: [],//图片列表
            bannerForm: {
                title: '',//轮播图名称
                img: '',//图片(文件)
                status: 1,//状态  1正常2禁用
            },
            formLabelWidth: '120px',
            rules: {
                title: [
                    { required: true, message: '请输入轮播图名称', trigger: 'blur' },
                    { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        ...mapActions({
            getBannerList: 'banner/getBannerlistAction',
        }),
        // 点击取消
        cancel() {
            this.$emit('cancel');
            this.reset();
            this.resetForm();
            // 清空图片
            this.fileList = [];
        },
        // 清空表单内容
        reset() {
            this.bannerForm = {
                title: '',//轮播图名称
                img: '',//图片(文件)
                status:1,//状态  1正常2禁用
            }
        },
        // 清空表单验证
        resetForm() {
            this.$refs.bannerRef.resetFields();
        },
        // 数据回显
        lookup(id) {
            bannerInfo({ id }).then(res => {
                if (res.code == 200) {
                    this.bannerForm = res.list;
                    this.bannerForm.id = id
                    // 回显图片
                    if (res.list.img) {
                        this.fileList = [{ url: this.bannerForm.img }]
                    }
                }
            })
        },
        // 添加
        add() {
            this.$refs.bannerRef.validate(val => {
                if (val) {
                    if (this.isAdd) {
                        // 添加
                        bannerAdd(this.bannerForm).then(res => {
                            if (res.code == 200) {
                                console.log(res);
                                // 添加成功
                                this.$message.success(res.msg);
                                // 关闭弹窗
                                this.cancel();
                                // 重新渲染视图
                                this.getBannerList();
                            } else {
                                // 添加失败
                                this.$message.error(res.msg);
                            }
                        })
                    } else {
                        // 修改
                        bannerEdit(this.bannerForm).then(res => {
                            if (res.code == 200) {

                                // 编辑成功
                                this.$message.success(res.msg);
                                // 关闭弹窗
                                this.cancel();
                                // 重新渲染视图
                                this.getBannerList();
                            } else {
                                // 编辑失败
                                this.$message.error(res.msg);
                            }
                        })
                    }
                }
            })

        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        // 预览图片
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            this.dialogVisible = true;
        },
        // 更改图片触发的函数
        changePhoto(file) {
            this.bannerForm.img = file.raw;
            console.log(file);
        },
    },
    mounted() { },
    watch: {},
    computed: {},
    filters: {},
    components: {},
}
</script>
<style scoped></style>